package trackListGenerator.id3Handler;

import com.mpatric.mp3agic.ID3v2;
import com.mpatric.mp3agic.ID3v22Tag;
import com.mpatric.mp3agic.InvalidDataException;
import com.mpatric.mp3agic.Mp3File;
import com.mpatric.mp3agic.NotSupportedException;
import com.mpatric.mp3agic.UnsupportedTagException;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import javax.imageio.ImageIO;

/**
 *
 * @author Aderthad
 */
public class ID3Handler {

    public static void main(String[] args) throws IOException, UnsupportedTagException, InvalidDataException, NotSupportedException {
        SetTags(new File("d:\\Aderthad\\Music\\_k\\Park In Hee\\Park In Hee - Golden (2005) (320 kbps)\\CD2\\"), 1);
    }

    private static void SetTags(File sourceFile, int i) throws IOException, UnsupportedTagException, InvalidDataException, NotSupportedException {
        if (!sourceFile.exists()) {
            System.out.println("File not found!");
            return;
        }

        File[] mp3s = sourceFile.listFiles(new FileFilter() {
            @Override
            public boolean accept(File file) {
                return file.getName().endsWith(".mp3");
            }
        });

        File image = new File(sourceFile.getCanonicalPath() + "\\cover.jpg");
        byte[] imageInByte = null;
        if (image.exists()) {
            BufferedImage bImage = ImageIO.read(image);
            try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
                ImageIO.write(bImage, "jpg", baos);
                baos.flush();
                imageInByte = baos.toByteArray();
            }
        }

        for (File mp3 : mp3s) {
            Mp3File mp3file = new Mp3File(mp3.getCanonicalPath());

            ID3v2 tag;

            if (mp3file.hasId3v2Tag()) {
                tag = mp3file.getId3v2Tag();
            } else {
                tag = new ID3v22Tag();
                mp3file.setId3v2Tag(tag);
            }

            String name = mp3.getName();

            if (i == -2) {
                final String separator = " ";
                int index = name.indexOf(separator);
                int track = Integer.parseInt(name.substring(0, index));
                String trackName = name.substring(index + separator.length(), name.length() - 4);

                tag.setTitle(trackName);
                tag.setTrack(String.valueOf(track));
                tag.setAlbum("2집 백만불짜리여자");
                tag.setAlbumArtist("뎁");
                tag.setArtist("뎁");
                tag.setYear("2011");
                tag.setGenreDescription("Rock");
                tag.setAlbumImage(imageInByte, "image/jpg");

                File newFolder = new File(sourceFile.getCanonicalPath() + "\\_NEW\\");
                newFolder.mkdir();
                mp3file.save(newFolder.getCanonicalPath() + "\\" + mp3.getName());

            } else if (i == -1) {
                if (tag.getAlbumImage() != null) {
                    byte[] im = tag.getAlbumImage();
                    try (ByteArrayInputStream bais = new ByteArrayInputStream(im)) {
                        BufferedImage bi = ImageIO.read(bais);
                        ImageIO.write(bi, "jpg", new File(sourceFile + "\\" + "__image.jpg"));
                        break;
                    }
                }

            } else if (i == 0) {
                /*int index = name.indexOf(" ");

                 if (index != -1) {
                 name = name.substring(index + 2);
                 }*/
                // name = name.substring(0, name.length() - 4);

                //tag.setAlbum(sourceFile.getParentFile().getName().substring(sourceFile.getParentFile().getName().indexOf("-") + 1));
                //tag.setAlbum(sourceFile.getName().substring(sourceFile.getName().indexOf("-") + 2));
                //tag.setGenreDescription("Indie");
                //tag.setArtist("로지피피 (Rossy Punky Perfume)");
                // tag.setAlbumArtist(sourceFile.getName().substring(0 ,sourceFile.getName().indexOf("-") - 1));
                // tag.setAlbum("Cozy Rossy Mini (EP)");
                // tag.setYear("2009");
                //   tag.setArtist("No Reply");
                // tag.setAlbumArtist("로지피피 (Rossy Punky Perfume)");
                //tag.setArtist(sourceFile.getParentFile().getName());
                //tag.setAlbumArtist(sourceFile.getParentFile().getName());
                //  tag.setAlbum("Songs For Ophelia (EP)");
                //  tag.setGenreDescription("New Age");
                //tag.setAlbumArtist("Standing Egg (스탠딩 에그)");
                // tag.setArtist("Standing Egg (스탠딩 에그)");
                //  tag.setAlbum("Innocence");
                //  tag.setGrouping(" ");
                // tag.setOriginalArtist(" ");
                //  tag.setPublisher(" ");
                //  tag.setUrl(" ");
                tag.setCopyright(" ");
                tag.setEncoder(" ");
                tag.setComment(" ");
                tag.setUrl(" ");
                //  tag.setGrouping(" ");
                tag.setAlbumImage(imageInByte, "image/jpg");

                File newFolder = new File(sourceFile.getCanonicalPath() + "\\_NEW\\");
                newFolder.mkdir();

                mp3file.save(newFolder.getCanonicalPath() + "\\" + mp3.getName());
            } else if (i == 1) {
                String track = tag.getTrack();
                int index = track.indexOf("/");
                if (index != -1) {
                    track = track.substring(0, index);
                }
                File newFolder = new File(sourceFile.getCanonicalPath() + "\\_NEW\\");
                newFolder.mkdir();
                mp3.renameTo(new File(sourceFile.getCanonicalPath() + "\\_NEW\\" + String.format("%02d", Integer.parseInt(track)) + ". " + tag.getTitle() + ".mp3"));
            } else if (i == 10) {
                System.out.println("v1 " + mp3file.hasId3v1Tag());
                System.out.println("custom " + mp3file.hasCustomTag());
                System.out.println("getTitle() " + tag.getTitle());
                System.out.println(tag.getAlbum());
                System.out.println("getAlbumArtist() " + tag.getAlbumArtist());
                System.out.println(tag.getArtist());
                System.out.println("tag.getComment() " + tag.getComment());
                System.out.println(tag.getComposer());
                System.out.println("getCopyright( )" + tag.getCopyright());
                System.out.println(tag.getGenre());
                System.out.println("getGenreDescription() " + tag.getGenreDescription());
                System.out.println(tag.getGrouping());
                System.out.println("getItunesComment() " + tag.getItunesComment());
                System.out.println(tag.getOriginalArtist());
                System.out.println("getPartOfSet() " + tag.getPartOfSet());
                System.out.println(tag.getPublisher());
                System.out.println("getUrl() " + tag.getUrl());
                System.out.println(tag.getEncoder());
                System.out.println(tag.getFrameSets().size());
            } else if (i == 11) {

                /*tag.setCopyright(" ");
                 tag.setComment(" ");
                 tag.setAlbumArtist("Standing Egg (스탠딩 에그)");
                 tag.setArtist("Standing Egg (스탠딩 에그)");
                 tag.setAlbum("36.5");*/
                tag.setComment(" ");
                File newFolder = new File(sourceFile.getCanonicalPath() + "\\_NEW\\");
                newFolder.mkdir();
                mp3file.save(newFolder.getCanonicalPath() + File.separator + mp3.getName());
                //mp3file.save(newFolder.getCanonicalPath() + File.separator + String.format("%02d", Integer.parseInt(tag.getTrack())) + ". " + mp3.getName());

            } else {
                String track = tag.getTrack();
                int index = track.indexOf("/");
                if (index != -1) {
                    track = track.substring(0, index);
                }
                track = String.format("%02d", Integer.parseInt(track));
                String trackName = mp3.getName();
                trackName = trackName.substring(name.indexOf("-") + 1);
                trackName = track + ". " + trackName;
                mp3.renameTo(new File(sourceFile.getCanonicalPath() + "\\" + trackName));
            }

        }
    }
}
